package com.ming.multifunction.controller;
import com.ming.multifunction.common.BaseResponse;
import com.ming.multifunction.common.DeleteRequest;
import com.ming.multifunction.common.ErrorCode;
import com.ming.multifunction.common.ResultUtils;
import com.ming.multifunction.exception.BusinessException;
import com.ming.multifunction.model.domain.Interest;
import com.ming.multifunction.model.domain.User;
import com.ming.multifunction.model.dto.PostQuery;
import com.ming.multifunction.model.request.InterestRequest;
import com.ming.multifunction.model.vo.InterestVO;
import com.ming.multifunction.service.InterestService;
import com.ming.multifunction.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 兴趣卡片
 */
@RestController
@RequestMapping("/friends")
public class FriendsController {

    @Resource
    private InterestService interestService;

    @Resource
    private UserService userService;

    /**
     * 展示兴趣卡片列表
     * @param postQuery
     * @param request
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<InterestVO>> selectTeam(PostQuery postQuery, HttpServletRequest request) {

        User loginUser = userService.getLoginUser(request);
        Long loginUserId = loginUser.getId();

        List<Interest> list = interestService.list();
        ArrayList<InterestVO> interestVOS = new ArrayList<>();

        for (Interest interest:list){
            if (interest==null){
                continue;
            }
            InterestVO interestVO = new InterestVO();
            BeanUtils.copyProperties(interest,interestVO);
            Long userId = interest.getUserId();
            if (userId==null){
                continue;
            }
            User user= userService.getById(userId);
            if (user==null){
                continue;
            }
            interestVO.setUsername(user.getUsername());
            interestVO.setUserAvatar(user.getAvatarUrl());
            interestVO.setUserId(userId);
            interestVO.setUserRole(user.getUserRole());
            interestVOS.add(interestVO);
        }

        return ResultUtils.success(interestVOS);
    }


    /**
     * 发布兴趣卡片
     * @param interest
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public BaseResponse<Long> publishPost(@RequestBody InterestRequest interest, HttpServletRequest request){

        if (interest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数不能为空");
        }

        User loginUser = userService.getLoginUser(request);
        if (loginUser==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"未登录");
        }

         long id = interestService.saveInterestPost(interest,loginUser);


        return ResultUtils.success(id);
    }

    /**
     * 推荐兴趣卡片
     * @param request
     * @return
     */
    @GetMapping("/recommend")
    public BaseResponse<List<InterestVO>> match(HttpServletRequest request){


        User loginUser = userService.getLoginUser(request);
        if (loginUser==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户未登录");
        }

        List<InterestVO> recommend= interestService.matchInterest(loginUser);

        return ResultUtils.success(recommend);
    }

    /**
     * 删除兴趣卡片
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {

        long id = deleteRequest.getId();
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = interestService.removeById(id);
        if (!b){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"删除失败");
        }
        return ResultUtils.success(b);
    }



}
